// pages/login/login.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    line:1,
    iphone:''
  },
  /**
   * 手机验证
   */
  iphonecheck:function(e){
    if (e.detail.value == '') {
      wx.showToast({
        title: '手机不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(e.detail.value))) {
      wx.showToast({
        title: '手机号码有误',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    this.setData({
      iphone:e.detail.value
    })
  },
  /**
   * 登录与注册之间的变换
   */
  change_model:function(e){
    // console.log(e.target.dataset.line)
    this.setData({
      line: e.target.dataset.line
    })
  },
  /**
   * 登录提交
   */
  formSubmit:function(e){
    var that = this;
    var formData = e.detail.value;
    var domain = app.globalData.domain;
    var PageInterface = app.globalData.PageInterface;
    var loginurl = domain + PageInterface;
    // console.log(formData);
    if(formData.acc_num==''){
      wx.showToast({
        title: '手机不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(formData.acc_num))) {
      wx.showToast({
        title: '手机号有误',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (formData.password == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (!(/^\w+$/.test(formData.password))){
      wx.showToast({
        title: '密码格式有误',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    wx.request({
      url: loginurl,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data:{
        nickname:formData.acc_num,
        password:formData.password,
        model:'MINI',
        api:'7004',
        format_type: 'json',
        api_cate: 'Personal',
      },
      success:function(res){
        if (res.data.status==1){
          if(res.data.msg){
            wx.showToast({
              title: res.data.msg,
              icon: 'loading',
              duration: 500
            })
          }else if(res.data.data){
            try {
              wx.setStorageSync('user_id', res.data.data.user_id )
              wx.setStorageSync('nickName', res.data.data.nickName)
              wx.setStorageSync('userAvatar', res.data.data.headimgurl)
              wx.removeStorageSync('share_id')
            } catch (e) {

            }
            app.globalData.inv_i = res.data.data.user_id                          
            wx.showToast({
              title: res.data.data.message,
              icon: 'loading',
              duration: 2000
            })
            wx.switchTab({
              url: '../personal/personal',
            })
          }
        }
      }
    })
  },
  /**
   * 验证码获取
   */
  button_get:function(){
    var that = this;
    var domain = app.globalData.domain;    
    var PageInterface = app.globalData.PageInterface;
    var codeurl = domain + PageInterface;
    wx.request({
      url: codeurl,
      method:'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data:{
        api: '7005',
        format_type: 'json',
        api_cate: 'Personal',
        model:'MINI',
      },
      success:function(res){
        if(res.data.status==1){
          wx.showToast({
            title: "'"+res.data.msg+"'",
            icon: 'success',
            duration: 5000
          })
        }
      }
    })
  },
  /**
   * 注册提交
   */
  formSubmit1:function(e){
    var that = this;
    var formData = e.detail.value;
    var domain = app.globalData.domain;
    var PageInterface = app.globalData.PageInterface;
    var setupurl = domain + PageInterface;
    if (formData.iphone == '') {
      wx.showToast({
        title: '手机不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    // if (!(/^1[34578]\d{9}$/.test(formData.iphone))) {
    //   wx.showToast({
    //     title: '手机号码有误',
    //     icon: 'loading',
    //     duration: 500
    //   })
    //   return false;
    // }
    if (formData.ver_in == '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (formData.pass_word == '') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    if (!(/^\w+$/.test(formData.pass_word))) {
      wx.showToast({
        title: '密码格式有误',
        icon: 'loading',
        duration: 500
      })
      return false;
    }
    wx.login({
      success:function(info){
        wx.getUserInfo({
          success:function(userinfo){
            var fromid = wx.getStorageSync('share_id')
            wx.request({
              url: setupurl,
              method: 'POST',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              data: {
                api: '7006',
                format_type: 'json',
                api_cate: 'Personal',
                model:"MINI",
                iphone: formData.iphone,
                ver_in: formData.ver_in,
                pass_word: formData.pass_word,
                cid: app.globalData.cid,
                code: info.code,
                encryptedData: userinfo.encryptedData,
                iv: userinfo.iv,
                fromid: fromid
              },
              success: function (res) {
                var jsonObj =  JSON.parse(res.data.trim())
                if (jsonObj.status==1){
                  if (jsonObj.msg){
                    wx.showToast({
                      title: jsonObj.msg,
                      icon: 'loading',
                      duration: 2000
                    })
                  } else if (jsonObj.data){
                    try{
                      wx.setStorageSync('user_id', jsonObj.data.user_id)
                      wx.setStorageSync('nickName', res.data.data.nickName)
                      wx.setStorageSync('userAvatar', res.data.data.headimgurl)
                      wx.removeStorageSync('share_id')
                    }catch(e){

                    }
                    app.globalData.inv_i = jsonObj.data.user_id
                    wx.switchTab({
                      url: '../personal/personal',
                    })
                  }
                }
              }
            })
           
          }
        })
      }
    })  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.share_id){
      wx.setStorage({
        key: "share_id",
        data: options.share_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})